package com.wzh.yebServe.config.security;
import com.wzh.yebServe.pojo.User;
import com.wzh.yebServe.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @Author wzh
 * @create 2022/11/6 13:54
 * @Description:实现自定义认证登录
 */
@Component
public class MyUserDetailService implements UserDetailsService {
    
    @Autowired
    private IUserService iUserService;

    /**
     * 为了实现自定义认证登录
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = iUserService.query().eq("username", username).one();
        if (user!=null){
         user.setRoles(iUserService.getRoles( user.getId()));
            return user;
        }
      throw new UsernameNotFoundException("用户名或者密码不正确");
    }
}
